<?php
if (!defined('SHARE_VERSION')) exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 项目说明：
// +----------------------------------------------------------------------
// | 本页功能：缓存类
// +----------------------------------------------------------------------
// | Copyright (c) 2008-2012 http://www.itye.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 吴锋(7462679@qq.com)
// +----------------------------------------------------------------------
class Cache {
	
	//缓存路径
	public	$cachedir =CACHE_PATH;
	
	//过期时间
	public $cachetime = 36000000;
	
	function __construct()
	{
		$this->cachedir	= CACHE_PATH;
		if(is_dir($this->cachedir))
		{//如果存在这个文件夹
			$this->cachedir = rtrim($this->cachedir,'/').'/';
		}
		else
		{
			die($this->cachedir.' cache dir is not exists.');
		}
	}
	
	/**
	* 设置过期时间
	*
	* @param integer $time  过期时间
	* @return boolean
	*/
	function setTime($time)
	{
		if(isset($cachetime) && is_integer($cachetime))
		{//检查$time是否为整数
			$this->time = $cachetime;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	* 读取缓存
	*
	* @param string $cache_id   要读取缓存的ID
	* @return mixed
	*/
	function read($cache_id) {
		$cache_file = $this->cachedir.$cache_id.'.php';//取得缓存的路径
		if(!file_exists($cache_file)) {//如果缓存不存在,返回false
			return false;
		}
		$mtime = filemtime($cache_file);//取得缓存的生成时间
		if((time() - $mtime) > $this->cachetime) {//判断缓存是否过期
			return false;
		} else {//读取缓存
			$fp = fopen($cache_file,'r');//打开缓存文件
			$content = fread($fp,filesize($cache_file));//读取缓存内容
			fclose($fp);
			unset($fp);
			if($content) {
				$content = $this->mb_unserialize($content);
				return $content;//返回反序列化的缓存内容
			} else {
				return false;
			}
		}
	}
	
	function mb_unserialize($serial_str) { 
		$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str ); 
		return unserialize ($out); 
	} 

	/**
	* 写入缓存
	*
	* @param string $content   缓存内容
	* @param string $cache_id  缓存ID
	* @return boolean
	*/
	function write($content,$cache_id) {
		$cache_file = $this->cachedir.$cache_id.'.php';//获得缓存文件名
		if(file_exists($cache_file)) {//如果存在相同ID的缓存,就把它删除
			@unlink($cache_file);
		}
		$fp = fopen($cache_file,'w');//创建缓存文件
		$yon = fwrite($fp,serialize($content));//写入序列化后的缓存内容
		fclose($fp);
		unset($fp);
		if ($yon){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	* 未使用格式的写入缓存
	*
	*
	*/
	function create($content,$file)
	{
		$cachefile = CACHE_PATH.$file.'.php';
		if(@file_exists($cachefile)){
			@unlink($cachefile);
		}
		$content="<?php\n\${$file}=".var_export($content,true)."\n?>";
		@!file_put_contents($cachefile,$content);
	}
	
	/**
	* 清除所有的缓存
	*
	* @param string $path
	* @return boolean
	*/
	function cleanCache($fileName = 'all') {
		$path = $this->cachedir;
		$path = rtrim($path,'/').'/';
		$fileName = $path.$fileName.'.php';
		if ( file_exists( $fileName ) ){
			return @unlink( $fileName );
		}else{
			return false;
		}
	}
	
	/*
		重写缓存
	*/
	function reloadCache($fileName = 'all')
	{
		$path = $this->cachedir;
		$path = rtrim($path,'/').'/';
		$fileName = $path.$fileName.'.php';
		if (touch($fileName))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}